//导入Ajax
import {
    ajax,
    isObject
} from "./ajax.js"
//导入放大镜
import Magnifier from "./magnifier.js"
var bname = unescape(window.location.href.split("?")[1].split("#")[0].split("=")[1]).split(".")
// 作者名字
var writer =""
//历史记录数组
var harr =[];
//添加历史记录
localStorage.setItem(bname[0]+'.'+bname[1]+'.'+bname[2] , "bookhistory")
// 设置网页标题
document.title = bname[2]
harr.push(bname[0]+'.'+bname[1]+'.'+bname[2])
for(var i = 1;i<localStorage.length;i++){
    if(localStorage.getItem(localStorage.key(i))=="bookhistory"){
        harr.push(localStorage.key(i))
    }
    if(harr.length>=10){
        harr.length=10;
        break;
    }
}


//调用ajax,进行初始化渲染*************************
ajax({
    url: "./json/book.json",
    dataType: "json",
    data: "",
    type: "get",
    cache: false,
    //调用成功执行函数
    success: function (data) {
        writer = data[bname[0]][bname[1]][bname[2]]["作者"];
        // 分类
        $(".include").html(`
                <span>${bname[0]}</span>
                <span>&gt;</span>
                <span>${bname[1]}</span>
                <span>&gt;</span>
                <span>${bname[2]}</span>
                `)

        // 放大镜图片
        $(".ori img")[0].src = data[bname[0]][bname[1]][bname[2]].url
        $(".large img")[0].src = data[bname[0]][bname[1]][bname[2]].url

        // 购物模板
        // 书名
        $(".bname h1").html(bname[2])
        // 定价
        $(".price-o b").html(data[bname[0]][bname[1]][bname[2]]["定价"])
        // 文轩价
        $(".price-n b").html(data[bname[0]][bname[1]][bname[2]]["文轩价"])
        // 作者
        $(".author a").html(data[bname[0]][bname[1]][bname[2]]["作者"])
        //所属分类
        $(".class dd").html(`
                <a href='#'>${bname[0]}</a>
                > <a href='#'>${bname[1]}</a>
                > <a href='#'>${bname[2]}</a>
                `)
        //书本信息
        $(".intro-cont ul").html(`
                <li>作　者：<a href="#" >${data[bname[0]][bname[1]][bname[2]]["作者"]}</a> </li>
                <li>出版社：<a  href="#">${data[bname[0]][bname[1]][bname[2]]["出版社"]}</a></li>
                <li>出版时间：${data[bname[0]][bname[1]][bname[2]]["出版时间"]}</li>
                <li>开　本：${data[bname[0]][bname[1]][bname[2]]["开本"]}</li>
                <li>页　数：${data[bname[0]][bname[1]][bname[2]]["页数"]}</li>
                <li>印刷时间：${data[bname[0]][bname[1]][bname[2]]["印刷时间"]}</li>
                <li>字　数：${data[bname[0]][bname[1]][bname[2]]["字数"]}</li>
                <li>装　帧：${data[bname[0]][bname[1]][bname[2]]["装帧"]}</li>
                <li>语　　种：${data[bname[0]][bname[1]][bname[2]]["语种"]}</li>
                <li>版　次：${data[bname[0]][bname[1]][bname[2]]["版次"]}</li>
                <li>印　次：${data[bname[0]][bname[1]][bname[2]]["印次"]}</li>
                <li>I S B N：${data[bname[0]][bname[1]][bname[2]]["ISBN"]}</li>
                `)

    },
    error: function (x) {
        alert(x)
    }
})
//调用ajax,进行初始化渲染*************************

//调用ajax,获取该作者的所有书和历史记录*************************
ajax({
    url: "./json/book.json",
    dataType: "json",
    data: "",
    type: "get",
    cache: false,
    //调用成功执行函数
    success: function (data) {
        // 创建数组，用来存放书籍
        var bookobj = {};
        //最多10本
        var i = 0;
        var ten = false;
        //遍历数据库
        for(var key in data){
            for(var attr in data[key]){
                for(var keyattr in data[key][attr]){
                    for(var key1 in data[key][attr][keyattr]){
                        if(data[key][attr][keyattr][key1]===writer){
                            if(bookobj[keyattr]){

                            }else{
                                i++;
                            }
                            bookobj[keyattr] = data[key][attr][keyattr];
                            bookobj[keyattr]['分类表'] = key + '.' + attr + '.' + keyattr;
                            if(i==10){
                                ten = true;
                                break
                            }
                        }
                    }
                    if(ten){
                        break
                    }
                }
                if(ten){
                    break
                }
            }
            
            if(ten){
                break
            }
        }
        //该作者的其他商品
        var str = ""
        for(var kk in bookobj){
            str +=`
            <div class="ano-book">
            <div>
                <img src=${bookobj[kk]["url"]} alt="">
            </div>
            <p><a href="./detail.html?bname=${escape(bookobj[kk]['分类表'])}">${kk}</a></p>
            <span>文轩价:</span>
            <b>${bookobj[kk]["文轩价"]}</b>
            </div>
            `
        }
        $(".another-cont").html(str+`
        <div class="ano-more">
        <p><a href="#">更多&gt;&gt;</a></p>
        </div>
        `)
        //历史记录
        var str1 = "";
        for(var i = 0;i<harr.length;i++){
            var book = harr[i].split(".")
            if(i==0){
                str1 +=`
                <div class="det-book">
                <div>
                    <img src=${data[book[0]][book[1]][book[2]]["url"]} alt="">
                </div>
                <p><a href="./detail.html?bname=${escape(harr[i])}">${book[2]}</a></p>
                <span>文轩价:</span>
                <b>${data[book[0]][book[1]][book[2]]["文轩价"]}</b>
                </div>
                `
            }else{
                str1 +=`
                <p class="det-p">
                <a href="./detail.html?bname=${escape(harr[i])}">* ${book[2]}</a>
                </p>
                `
            }
        }
        $(".det-history-cont").html(str1)


    },
    error: function (x) {
        alert(x)
    }
})
//调用ajax,获取该作者的所有书和历史记录*************************



// 实例放大镜
new Magnifier($(".ori")[0], $(".large")[0], $(".fil")[0])
//给回到顶部添加点击事件
$(".totop").on("click", function () {
    document.documentElement.scrollTop = 0
})
window.addEventListener("scroll", function () {
    if (document.documentElement.scrollTop >= 300) {
        $(".totop")[0].style.display = "block"
    } else {
        $(".totop")[0].style.display = "none"
    }
})

//添加购物车
$(".buy-active a").on("click",function(){
//添加历史记录
localStorage.setItem("c."+bname[0]+'.'+bname[1]+'.'+bname[2] , "bookcar")
//要购买的数量
var n = $(".buy-num-text").val()
//添加历史记录
localStorage.setItem("cnum."+bname[0]+'.'+bname[1]+'.'+bname[2] , n)
alert("已成功添加到购物车")
})

//改变购买数量
$(".reduce-btn").on("click",function(){
    if($(".buy-num-text").val()<=1){
        $(".buy-num-text").val(1)
        return
    }
    var a= parseInt($(".buy-num-text").val())-1
    $(".buy-num-text").val(a)
})
$(".add-btn").on("click",function(){
    var a= parseInt($(".buy-num-text").val())+1
    $(".buy-num-text").val(a)
})